#include "./button.h"
#include "ui/image.h"
#include "ui/text.h"

using namespace std;
using namespace MiniUI;
using namespace MiniUI::Type;

const char* DeriveUIObject<Button, View>::s_szTypeName = "Button";

Button::Button()
: m_pImg(nullptr)
, m_pText(nullptr)
{
}

Button::~Button()
{
    m_pImg->Release();
    m_pText->Release();
}

void Button::Init(const string& strNormal,
                  const string& strHover,
                  const string& strDown)
{
    m_strNormal = strNormal;
    m_strHover = strHover;
    m_strDown = strDown;

    m_pImg = Image::CreateInstance();
    this->AddChild(m_pImg);

    m_pImg->SetTransparent(true);
    m_pImg->SetValue(strNormal.c_str());

    MiniSize size;
    m_pImg->GetSize(size);
    this->SetSize(size);

    m_pImg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    m_pImg->SetMargin(MiniRect(0, 0, 0, 0));
}

void Button::SetText(const string& strText)
{
    if (!m_pText)
    {
        m_pText = Text::CreateInstance();
        this->AddChild(m_pText);
        m_pText->SetTransparent(true);
        m_pText->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
        m_pText->SetMargin(MiniRect(0, 0, 0, 0));
        m_pText->SetTextAlign(TEXTALIGN_CENTER | TEXTALIGN_VCENTER);
    }

    m_pText->SetValue(strText);
    MiniRect rcButton(0, 0, m_size.cx, m_size.cy);
    DoChange(rcButton);
}

void Button::MouseEnter()
{
    m_pImg->SetValue(m_strHover.c_str());
    m_pImg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    m_pImg->SetMargin(MiniRect(0, 0, 0, 0));
    MiniRect rcButton(0, 0, m_size.cx, m_size.cy);
    DoChange(rcButton);
}

void Button::MouseLeave()
{
    m_pImg->SetValue(m_strNormal.c_str());
    m_pImg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    m_pImg->SetMargin(MiniRect(0, 0, 0, 0));
    MiniRect rcButton(0, 0, m_size.cx, m_size.cy);
    DoChange(rcButton);
}

bool Button::MouseUp(const Type::MiniPoint& pt, MouseButton button, uint keyState, bool& bHandled)
{
    bHandled = true;
    MiniRect rcButton(0, 0, m_size.cx, m_size.cy);
    m_pImg->SetValue(rcButton.PtInRect(pt) ? m_strHover.c_str() : m_strNormal.c_str());
    m_pImg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    m_pImg->SetMargin(MiniRect(0, 0, 0, 0));
    DoChange(rcButton);
    return true;
}

bool Button::MouseDown(const MiniPoint& pt, MouseButton button, uint keyState, bool& bHandled)
{
    bHandled = true;
    m_pImg->SetValue(m_strDown.c_str());
    m_pImg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    m_pImg->SetMargin(MiniRect(0, 0, 0, 0));
    MiniRect rcButton(0, 0, m_size.cx, m_size.cy);
    DoChange(rcButton);
    return true;
}